﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //        Response.Write("<script language='javascript'>");
        //        Response.Write(@"function iniWindow() {
        //            var opener = window.dialogArguments;
        //            var FlowXML = opener.document.all.FlowXML;	 
        //            var xmlDoc = new ActiveXObject('MSXML2.DOMDocument');
        //            xmlDoc.async = false;
        //            xmlDoc.loadXML(FlowXML.value);
        //            var xmlRoot = xmlDoc.documentElement;
        //            var Flow = xmlRoot.getElementsByTagName('FlowConfig').item(0);
        //            filename = Flow.getElementsByTagName('BaseProperties').item(0).getAttribute('flowId') + '.xml';
        //            document.all.Hidden1.value = FlowXML.value;
        //        }");
        //Response.Write("</script>");
        //Button1_Click(sender, null);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string strsummary = "";// Hidden1.Value.Trim();
        string strtitle = Request.QueryString["id"];
        string strname = "";// Hidden2.Value.Trim();
        string path = Server.MapPath("flows") + @"\" + strtitle.Trim() + ".xml";
        FileInfo fi = new FileInfo(path);
        if (!fi.Exists)
        {
            using (StreamWriter sw = fi.CreateText())
            {
                sw.WriteLine(@"<?xml version='1.0' encoding='UTF-8'?>");
                sw.WriteLine("" + strsummary.Trim() + "");
            }
        }
        else
        {
            StreamWriter fw = new StreamWriter(path);
            fw.WriteLine(@"<?xml version='1.0' encoding='UTF-8'?>");
            fw.WriteLine("" + strsummary.Trim() + "");
            fw.Close();
        }
        string path2 = Server.MapPath("langs/main_zh.xml");
        FileInfo fi2 = new FileInfo(path2);
        if (fi2.Exists)
        {
            StreamWriter sw2 = new StreamWriter(path2);
            sw2.WriteLine(@"<?xml version='1.0' encoding='utf-8'?><config><Properties><item id='treeText' property='innerHTML' text='流程导航视图' />
            <item id='btnNewFlow' property='title' text='创建新流程' />
            <item id='btnEditFlow' property='title' text='修改流程' /><item id='btnSaveFlow' property='title' text='导出流程' />
            </Properties><Selects><select id='flowList'>");
            string DirPath = Server.MapPath("flows");
            DirectoryInfo di = new DirectoryInfo(DirPath);
            FileInfo[] dis = di.GetFiles();
            if (dis.Length > 1)
            {
                for (int i = 0; i < dis.Length; i++)
                {
                    if (dis[i].Name.ToString().Substring(dis[i].Name.LastIndexOf(".") + 1) == "xml")
                    {
                        StreamReader SR = new StreamReader(DirPath + "/" + dis[i].Name);
                        string s = SR.ReadToEnd();
                        string flowId = string.Empty;
                        string flowText = string.Empty;
                        if (s.Contains("flowId"))
                        {
                            s = s.Replace("flowId", "$");
                            flowId = s.Split(new char[] { '$' }, 2, StringSplitOptions.RemoveEmptyEntries).GetValue(1).ToString().Trim();
                            flowId = flowId.Split(new char[] { '"' }, 10, StringSplitOptions.RemoveEmptyEntries).GetValue(1).ToString().Trim();
                        }
                        if (s.Contains("flowText"))
                        {
                            s = s.Replace("flowText", "%");
                            flowText = s.Split(new char[] { '%' }, 2, StringSplitOptions.RemoveEmptyEntries).GetValue(1).ToString().Trim();
                            flowText = flowText.Split(new char[] { '"' }, 10, StringSplitOptions.RemoveEmptyEntries).GetValue(1).ToString().Trim();
                        }
                        sw2.WriteLine(@"<option value='" + flowId + ".xml' text='" + flowText + "' />");
                    }
                }
            }
            sw2.WriteLine(@"</select></Selects></config>");
            sw2.Close();
        }
        Response.Write("<script language='javascript'>window.close();</script>");
    }
}
